layui.use(['table', 'form', 'laydate'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$
    var tableJobsEx
    var listUrls = jsRoutes.controllers.admin.TasksController.listJobExecutions();
    var removeUrls = jsRoutes.controllers.admin.TasksController.removeJobExecutions();
    form.on('submit(jsonJobsEx)', function (data) {
        //layer.msg(JSON.stringify(data.field));
        tableJobsEx = table.render({
            elem: '#tableJobsEx'
            , where: data.field
            , height: 550
            , method: listUrls.method
            , url: listUrls.url //数据接口
            , page: true //开启分页
            , limits: [10, 20, 50, 100, 200, 500, 1000]
            , toolbar: 'default'
            , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }
                , { field: 'id', title: 'ID', width: 90, sort: true, fixed: 'left' }
                , { field: 'job_id', title: '任务ID', width: 100 }
                , { field: 'name', title: '名称', width: 120, sort: true }
                , { field: 'status', title: '状态', width: 80 }
                , { field: 'started', title: '开始时间', width: 210, sort: true }
                , { field: 'finished', title: '结束时间', width: 210 }
                , { field: 'runtime', title: '运行时长', width: 90 }
                , { field: 'exception', title: '信息', width: 400, sort: true }
                , { field: 'traceback', title: '异常', width: 100, sort: true }

            ]]
        });
        return false;
    });

    //监听头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                layer.msg('添加');
                break;
            case 'update':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    layer.msg('删除');
                    console.log(data)

                    var userArr = []
                    for (i = 0; i < data.length; i++) { userArr.push(data[i].id); }
                    $.post({
                        url: removeUrls.url,
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify({ option: "delete", key: "id", id: userArr }),
                        dataType: 'text',
                        success: function (data) {
                            console.log(data);
                            alert("数据: \n" + data + "\n状态: " + status);
                            tableJobsEx.reload();
                        }
                    });

                }
                break;
        };
    });


    var laydate = layui.laydate;
    //执行一个laydate实例
    laydate.render({
        elem: '#datetimerev' //指定元素
        , type: 'datetime'
        , range: '~' //true
        , format: 'yyyy-MM-ddTHH:mm:ss'
    });
});